package com.baselibrary.libs.Utils.UtilsMyText;

import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import com.baselibrary.libs.Utils.Toast.UtilsToast;

import java.util.ArrayList;

/**
 * Created by 35876 于萌萌
 * 创建日期: 8:57 . 2016年11月10日
 * 描述:自己的  字符串工具类
 * <p>
 * <p>
 * 备注:
 */

public class UtilsMyText {


    /**
     * 判断 textView 是否为空
     *
     * @param textView
     * @return
     */
    public static Boolean isEmptys(TextView textView) {
        return TextUtils.isEmpty(textView.getText().toString());
    }

    /**
     * 获取 textView 里面的东西
     *
     * @param textView
     * @return
     */
    public static String getTextView(TextView textView) {
        if (TextUtils.isEmpty(textView.getText().toString())) {
            return "";
        } else {
            return textView.getText().toString().trim();

        }

    }

    /**
     * 毫秒转换成 日 是分 喵
     * @param ms
     * @return
     */
    public static String formatTime(long ms) {

        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        String strDay = day < 10 ? "0" + day : "" + day; //天
        String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
        String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
        String strSecond = second < 10 ? "0" + second : "" + second;//秒
        String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;

        return strDay +"日"+strHour+"小时"+strMinute + " 分钟 " + strSecond + " 秒";
    }

    /**
     * 格式化时间
     * @param ms
     * @return
     */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分");
        }
        if(second > 0) {
            sb.append(second+"秒");
        }
        if(milliSecond > 0) {
            sb.append(milliSecond+"毫秒");
        }
        return sb.toString();
    }

    /**
     * 隐藏四位电话号码
     *
     * @param phone
     * @return
     */
    public static String hide4Num(String phone) {
        // String phone2 = "15988888888";
        if (!TextUtils.isEmpty(phone)) {
            return phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
        } else {
            return "";
        }

    }

    /**
     * 获取字符串的第一个字符串
     *
     * @param string
     * @return
     */
    public static String getfristString(String string) {
        if (!TextUtils.isEmpty(string)) {
            return string.substring(0, 1);
        } else {
            return "";
        }

    }

    /**
     * 获取保留两位的 字符串 浮点数
     *
     * @param doubleString 想要保留的浮点数 字符串
     * @return
     */
    public static String get2DoubleString(String doubleString) {
        if (!TextUtils.isEmpty(doubleString)) {
            double v = Double.parseDouble(doubleString);
            return String.format("%.2f", v);
        } else {
            return "";
        }
    }

    /**
     * 获取字符串或者 textview 长度
     *
     * @param textView
     * @return
     */
    public static int getLengh(TextView textView) {
        if (TextUtils.isEmpty(textView.toString())) {
            return 0;
        } else {
            return textView.getText().toString().length();
        }
    }

    /**
     * 对比两个  字符串的 内容 是否一样
     *
     * @param s1
     * @param s2
     * @return
     */
    public static boolean isEquals(String s1, String s2) {
        if (null == s1 || null == s2) {
            return false;
        }
        if (s1.equals(s2)) {
            return true;
        } else {

            return false;
        }
    }


    public static void setHintAddText(TextView textView, String s) {
        textView.setText(textView.getHint().toString() + s);
    }

    public static void setText(TextView textView, String s) {
        textView.setText(s);
    }

    /**
     * 获取限制 text 输入内容的char[] 主要是没有空格
     *
     * @return
     */
    public static char[] getChar() {
        return new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
                , 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'
                , 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'
                , 'z', 'x', 'c', 'v', 'b', 'n', 'm'
                , 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'
                , 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'
                , 'Z', 'X', 'C', 'V', 'B', 'N', 'M'
                , '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '=', '_', '+'
                , '[', ']', '{', '}', ';', ':', ',', '.', '<', '>', '/', '?'
        };
    }

    public static Rule getRule(Context context) {
        return new Rule(context);
    }

    /**
     * 一个专门设置 规则的类
     * 大于小于
     */
    public static class Rule {
        Context context;
        ArrayList<String> mStrings;
        ArrayList<setRule> mSetRules = new ArrayList<>();

        public Rule(Context context) {
            this.context =context;
        }


        public Rule setList(ArrayList<String> list) {
            this.mStrings = list;
            return this;
        }

        public Rule setRule_Xiao_Yu_Deng_Yu
                (int listNum, int less_than_or_equal_to, String error) {
            mSetRules.add(new setRule(0, listNum, less_than_or_equal_to, error));
            return this;
        }

        public Rule setRule_Xiao_Yu(int listNum, int less_than, String error) {
            mSetRules.add(new setRule(1, listNum, less_than, error));
            return this;
        }

        public Rule setRule_Deng_Yu(int listNum1, int listNum2, String error) {
            mSetRules.add(new setRule(2, listNum1, listNum2, error));
            return this;
        }

        public Rule setRule_Da_Yu(int listNum, int greater, String error) {
            mSetRules.add(new setRule(3, listNum, greater, error));
            return this;
        }

        public Rule setRule_Da_Yu_Deng_Yu(int listNum, int great_than_or_equal_to, String error) {
            mSetRules.add(new setRule(4, listNum, great_than_or_equal_to, error));
            return this;
        }

        public Rule setRule_Equals(int listNum1, int listNum2, String error) {
            mSetRules.add(new setRule(5, listNum1, listNum2, error));
            return this;
        }
        public Rule setRule_Bu_Deng_yu(int listNum1, int listNum2, String error) {
            mSetRules.add(new setRule(6, listNum1, listNum2, error));
            return this;
        }



        public int show() {
            int errornum = 2;
            for (int i = 0; i < mStrings.size(); i++) {
                //String s = mStrings.get(i);
                for (int j = 0; j < mSetRules.size(); j++) {
                    setRule mRule = mSetRules.get(j);

                    switch (mRule.tag) {
                        //小于等于
                        case 0:
                            if (( mStrings.get(mRule.listNum1).length() <= mRule.listNum2)) {
                                UtilsToast.showToast(context, mRule.error);
                                return errornum;
                            }
                            break;
                        //小于
                        case 1:
                            if (( mStrings.get(mRule.listNum1).length() < mRule.listNum2)) {
                                UtilsToast.showToast(context, mRule.error);
                                return errornum;
                            }
                            break;
                        //等于
                        case 2:
                            if (( mStrings.get(mRule.listNum1).length() == mRule.listNum2)) {
                                UtilsToast.showToast(context, mRule.error);
                                return errornum;
                            }
                            break;
                        //大于
                        case 3:
                            if (( mStrings.get(mRule.listNum1).length() > mRule.listNum2)) {
                                UtilsToast.showToast(context, mRule.error);
                                return errornum;
                            }
                            break;
                        //大于等于
                        case 4:
                            if (( mStrings.get(mRule.listNum1).length() >= mRule.listNum2)) {
                                UtilsToast.showToast(context, mRule.error);
                                return errornum;
                            }
                            break;
                        //两个内容是否一样
                        case 5:
                            if (!UtilsMyText.isEquals(mStrings.get(mRule.listNum1), mStrings.get(mRule.listNum2))) {
                                UtilsToast.showToast(context, mRule.error);
                                return errornum;
                            }
                            break;
                        //不等于
                        case 6:
                            if (mStrings.get(mRule.listNum1).length() != mRule.listNum2) {
                                UtilsToast.showToast(context, mRule.error);
                                return errornum;
                            }
                            break;
                    }
                }
            }
            return 0;
        }

    }

    static class setRule {
        public int tag;
        public int listNum1;
        public int listNum2;
        public String error;

        public setRule(int tag, int listNum1, int listNum2, String error) {
            this.tag = tag;
            this.listNum1 = listNum1;
            this.listNum2 = listNum2;
            this.error = error;
        }
    }
}
